package com.itheima.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.Setmeal;
import com.itheima.pojo.User;
import com.itheima.service.MemberService;
import com.itheima.utils.DateUtils;
import com.itheima.utils.GetAge;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.util.*;

/**
 * 会员实现类
 */
@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private SetmealDao setmealDao;
    //分页查询


//    @Override
//    public PageResult pageQuery(QueryPageBean queryPageBean) {
//        //获取分页参数
//        Integer currentPage = queryPageBean.getCurrentPage();
//        Integer pageSize = queryPageBean.getPageSize();
//        String queryString = queryPageBean.getQueryString();
//        //使用分页助手分页
//        PageHelper.startPage(currentPage, pageSize);
//        //调用dao查询根据条件查询
//        Page<Member> page = memberDao.selectByCondition(queryString);
//        return new PageResult(page.getTotal(), page.getResult());
//    }

    //新增会员
    @Override
    public void addMember(Member member) {
        member.setRegTime(new Date());
        if (member.getPassword() != null) {
            member.setPassword(MD5Utils.md5(member.getPassword()));
        }
        memberDao.add(member);
    }
    //编辑回显数据(根据id查询)
//    @Override
//    public Member findById(Integer id) {
//        return memberDao.findById(id);
//    }

    //编辑更新会员数据
//    @Override
//    public void edit(Member member) {
//         memberDao.editMember(member);
//    }
    //根据Id删除会员数
//    @Override
//    public void delete(Integer id) {
//        memberDao.deleteById(id);
//    }
    //根据会员id查询预约信息
    @Override
    public List<Order> findOrderIdByMember(Integer id) {
        List<Order> byMemberId = orderDao.findByMemberId(id);
        for (Order order : byMemberId) {
            System.out.println(order.toString());
        }
        //查询预约信息
        return orderDao.findByMemberId(id);
    }

    @Override
    public Setmeal findSetmealDetail(Integer id) {
        return setmealDao.findDetailsById(id);
    }

    //根据电话号码查询会员
    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    //注册会员
//    @Override
//    public void add(Member member) {
//        String password = member.getPassword();
//        if(password!=null){
//            password= MD5Utils.md5(password);
//            member.setPassword(password);
//        }
//        memberDao.add(member);
//    }

    @Override
    public Result findByAge() {
        Integer infant   = memberDao.infant();
        Integer children = memberDao.children();
        Integer teenager = memberDao.teenager();
        Integer youth    = memberDao.youth();
        Integer middle   = memberDao.middle();
        Integer agedness = memberDao.agedness();
        List<Integer> list = new ArrayList<>();
        list.add(infant );
        list.add(children);
        list.add(teenager);
        list.add(youth);
        list.add(middle);
        list.add(agedness);
        return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,list);
    }




    @Override
    public Member findMemberByIdCardAndPwd(String idCard, String password) {
        Member member = memberDao.findMemberByIdCardAndPwd(idCard,password);
        return member;
    }

    @Override
    public List<Map> findSetmealById(int id) {
        List<Map> list= memberDao.findSetmealById(id);
        return list;
    }

    @Override
    public String findPasswordById(String id) {
        return memberDao.findPasswordById(Integer.parseInt(id));
    }


    @Override
    public void editPassword(Map map) {
        memberDao.editPassword(map);
    }

    @Override
    public List<Map> findSetmealByIdAndDate(Map map) throws Exception {
        int id = Integer.parseInt((String) map.get("id"));
        Date startDate = DateUtils.parseString2Date((String) map.get("startDate"));
        Date endDate = DateUtils.parseString2Date((String) map.get("endDate"));
        Map newMap=new HashMap();
        newMap.put("id",id);
        newMap.put("startDate",startDate);
        newMap.put("endDate",endDate);
        List<Map> list = memberDao.findSetmealByIdAndDate(newMap);
        return list;
    }

    @Override
    public void edit2Telephone(Map map) {
        memberDao.edit2Telephone(map);
    }




    //刘浩然
    //根据电话号码查询会员
//    @Override
//    public Member findByTelephone(String telephone) {
//        return memberDao.findByTelephone(telephone);
//    }

    //注册会员
    @Override
    public void add(Member member) {
        String password = member.getPassword();
        if (password != null) {
            member.setPassword(MD5Utils.md5(password));
        }
        member.setRegTime(new Date());
        memberDao.add(member);
    }

    //会员分页
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {


        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();//条件查询
        Date startTime = queryPageBean.getStartTime();//开始时间
        Date endTime = queryPageBean.getEndTime();//结束时间
        Map<String, Object> map = new HashMap<>();
        map.put("queryString", queryString);
        map.put("startTime", startTime);
        map.put("endTime", endTime);
        //将数据给分页插件z
        PageHelper.startPage(currentPage, pageSize);

        //查出的数据放在page对象中
        Page<Member> page = memberDao.selectByCondition(map);

        //memberDao.selectByTime(startTime,endTime);
        //循环查出来的数据
        for (Member member : page) {
            //判断客户是否填写了生日时间
            if (member.getBirthday() != null) {
                //如果填写了,将生日信息交给GetAge工具类处理拿到客户生日
                try {
                    int ageByBirth = GetAge.getAgeByBirth(member.getBirthday());
                    //将客户年龄放入member类
                    Integer m=new Integer(ageByBirth);

                    member.setAge(m.toString());
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            //获得健康管理师id
            Integer theHealthManagerId = member.getHealthManagerId();
            //判断客户是否预约了管理师
            if (theHealthManagerId != null) {
                String healthManager = memberDao.findHealthManager(theHealthManagerId);
                member.setHealth(healthManager);
            }
        }
        //将page中的总条数和每页的数据封装进PageResult
        return new PageResult(page.getTotal(), page.getResult());


    }

    @Override
    public List<User> findUser() {
        return memberDao.findUser();
    }

    @Override
    public Member findById(Integer id) {
        return memberDao.findById(id);
    }

    //删除功能
    @Override
    public void delete(Integer id) {
        //先判断是否和order表有关联的数据，有的话就不可以删除
        long count = memberDao.findCountByMemberId(id);
        if (count > 0) {
            throw new RuntimeException(MessageConstant.DELETE_MEMBER_FAIL);
        } else {
            memberDao.deleteById(id);
        }
    }

    @Override
    public void edit(Map map) {
        String birthday = (String)map.get("birthday");
        System.out.println(birthday);
        String b = birthday.split("T")[0];
        map.put("birthday",b);

        memberDao.edit(map);
    }

    @Override
    public int findCountByMemberId(int id) {
        return memberDao.findCountByMemberId(id);
    }

    @Override
    public List<Integer> findMemberCountByMonths(List<String> months) {
        return null;
    }

    @Override
    public List<Map> getAgeStatistics() {
        return memberDao.findMemberAge();
    }

    @Override
    public Map getMemberReport() throws Exception {
        List<Member> members = memberDao.findAll();
        //年龄的分层
        int young1 = 0;
        int puber1 = 0;
        int wrinkly1 = 0;
        int quinquagenarian1 = 0;
        int oldpeople1 = 0;


        int young2 = 0;
        int puber2 = 0;
        int wrinkly2 = 0;
        int quinquagenarian2 = 0;
        int oldpeople2 = 0;

        int young3 = 0;
        int puber3 = 0;
        int wrinkly3 = 0;
        int quinquagenarian3 = 0;
        int oldpeople3 = 0;

        int young4 = 0;
        int puber4 = 0;
        int wrinkly4 = 0;
        int quinquagenarian4 = 0;
        int oldpeople4 = 0;

        int young5 = 0;
        int puber5 = 0;
        int wrinkly5 = 0;
        int quinquagenarian5 = 0;
        int oldpeople5 = 0;

        int young6 = 0;
        int puber6 = 0;
        int wrinkly6 = 0;
        int quinquagenarian6 = 0;
        int oldpeople6 = 0;

        int young7 = 0;
        int puber7 = 0;
        int wrinkly7 = 0;
        int quinquagenarian7 = 0;
        int oldpeople7 = 0;

        int young8 = 0;
        int puber8 = 0;
        int wrinkly8 = 0;
        int quinquagenarian8 = 0;
        int oldpeople8 = 0;

        int young9 = 0;
        int puber9 = 0;
        int wrinkly9 = 0;
        int quinquagenarian9 = 0;
        int oldpeople9 = 0;


        //年月的分层
        List<Member> y12 = new ArrayList<>();
        List<Member> y13 = new ArrayList<>();
        List<Member> y14 = new ArrayList<>();
        List<Member> y15 = new ArrayList<>();
        List<Member> y16 = new ArrayList<>();
        List<Member> y17 = new ArrayList<>();
        List<Member> y18 = new ArrayList<>();
        List<Member> y19 = new ArrayList<>();
        List<Member> y20 = new ArrayList<>();


        //年级
        List<Object> yearList = new ArrayList<>();
        List<Object> young = new ArrayList<>();
        List<Object> puber = new ArrayList<>();
        List<Object> wrinkly = new ArrayList<>();
        List<Object> quinquagenarian = new ArrayList<>();
        List<Object> oldpeople = new ArrayList<>();


        for (Member member : members) {
            Date regTime = member.getRegTime();
            if (regTime!=null){
                String[] split = regTime.toString().split(" ");

                if (split[split.length-1].equals("2012")) {
                    y12.add(member);
                } else if (split[split.length-1].equals("2013")) {
                    y13.add(member);
                } else if (split[split.length-1].equals("2014")) {
                    y14.add(member);
                } else if (split[split.length-1].equals("2015")) {
                    y15.add(member);
                } else if (split[split.length-1].equals("2016")) {
                    y16.add(member);
                } else if (split[split.length-1].equals("2017")) {
                    y17.add(member);
                } else if (split[split.length-1].equals("2018")) {
                    y18.add(member);
                } else if (split[split.length-1].equals("2019")) {
                    y19.add(member);
                } else if (split[split.length-1].equals("2020")) {
                    y20.add(member);
                }
            }

        }






        yearList.add("product");
        yearList.add("2012");
        yearList.add("2013");
        yearList.add("2014");
        yearList.add("2015");
        yearList.add("2016");
        yearList.add("2017");
        yearList.add("2018");
        yearList.add("2019");
        yearList.add("2020");


        for (Member y12m : y12) {
            Date birthday12 = y12m.getBirthday();
            if (birthday12!=null){
                int age12 = GetAge.getAgeByBirth(birthday12);
                if (age12 >= 15 && age12 <= 30) {

                    young1++;
                } else if (age12 >= 30 && age12 <= 45) {
                    puber1++;
                } else if (age12 >= 45 && age12 <= 60) {
                    wrinkly1++;
                } else if (age12 >= 60 && age12 < +75) {
                    quinquagenarian1++;
                } else if (age12 >= 75 && age12 <= 90) {
                    oldpeople1++;
                }
            }}

        for (Member y13m : y13) {
            Date birthday13 = y13m.getBirthday();

            if (birthday13!=null){
                int age13 = GetAge.getAgeByBirth(birthday13);
                if (age13 >= 15 && age13 <= 30) {
                    young2++;
                } else if (age13 >= 30 && age13 <= 45) {
                    puber2++;
                } else if (age13 >= 45 && age13 <= 60) {
                    wrinkly2++;
                } else if (age13 >= 60 && age13 < +75) {
                    quinquagenarian2++;
                } else if (age13 >= 75 && age13 <= 90) {
                    oldpeople2++;
                }
            }}


        for (Member y14m : y14) {
            Date birthday14 = y14m.getBirthday();
            if (birthday14!=null){
                int age14 = GetAge.getAgeByBirth(birthday14);
                if (age14 >= 15 && age14 <= 30) {
                    young3++;
                } else if (age14 >= 30 && age14 <= 45) {
                    puber3++;
                } else if (age14 >= 45 && age14 <= 60) {
                    wrinkly3++;
                } else if (age14 >= 60 && age14 < +75) {
                    quinquagenarian3++;
                } else if (age14 >= 75 && age14 <= 90) {
                    oldpeople3++;
                }
            }}

        for (Member y15m : y15) {
            Date birthday15 = y15m.getBirthday();
            if (birthday15!=null){
                int age15 = GetAge.getAgeByBirth(birthday15);
                if (age15 >= 15 && age15 <= 30) {
                    young4++;
                } else if (age15 >= 30 && age15 <= 45) {
                    puber4++;
                } else if (age15 >= 45 && age15 <= 60) {
                    wrinkly4++;
                } else if (age15 >= 60 && age15 < +75) {
                    quinquagenarian4++;
                } else if (age15 >= 75 && age15 <= 90) {
                    oldpeople4++;
                }
            }}


        for (Member y16m : y16) {
            Date birthday16 = y16m.getBirthday();
            if (birthday16!=null){
                int age16 = GetAge.getAgeByBirth(birthday16);
                if (age16 >= 15 && age16 <= 30) {
                    young5++;
                } else if (age16 >= 30 && age16 <= 45) {
                    puber5++;
                } else if (age16 >= 45 && age16 <= 60) {
                    wrinkly5++;
                } else if (age16 >= 60 && age16 < +75) {
                    quinquagenarian5++;
                } else if (age16 >= 75 && age16 <= 90) {
                    oldpeople5++;
                }
            }}


        for (Member y17m : y17) {
            Date birthday17 = y17m.getBirthday();
            if (birthday17!=null){
                int age17 = GetAge.getAgeByBirth(birthday17);
                if (age17 >= 15 && age17 <= 30) {
                    young6++;
                } else if (age17 >= 30 && age17 <= 45) {
                    puber6++;
                } else if (age17 >= 45 && age17 <= 60) {
                    wrinkly6++;
                } else if (age17 >= 60 && age17 < +75) {
                    quinquagenarian6++;
                } else if (age17 >= 75 && age17 <= 90) {
                    oldpeople6++;
                }
            }}


        for (Member y18m : y18) {
            Date birthday18 = y18m.getBirthday();
            if (birthday18!=null){
                int age18 = GetAge.getAgeByBirth(birthday18);
                if (age18 >= 15 && age18 <= 30) {
                    young7++;
                } else if (age18 >= 30 && age18 <= 45) {
                    puber7++;
                } else if (age18 >= 45 && age18 <= 60) {
                    wrinkly7++;
                } else if (age18 >= 60 && age18 < +75) {
                    quinquagenarian7++;
                } else if (age18 >= 75 && age18 <= 90) {
                    oldpeople7++;
                }
            }}


        for (Member y19m : y19) {
            Date birthday19 = y19m.getBirthday();
            if (birthday19!=null){
                int age19 = GetAge.getAgeByBirth(birthday19);
                if (age19 >= 15 && age19 <= 30) {
                    young8++;
                } else if (age19 >= 30 && age19 <= 45) {
                    puber8++;
                } else if (age19 >= 45 && age19 <= 60) {
                    wrinkly8++;
                } else if (age19 >= 60 && age19 < +75) {
                    quinquagenarian8++;
                } else if (age19 >= 75 && age19 <= 90) {
                    oldpeople8++;
                }
            }}


        for (Member y20m : y20) {
            Date birthday20 = y20m.getBirthday();
            if (birthday20!=null){
                int age20 = GetAge.getAgeByBirth(birthday20);
                if (age20 >= 15 && age20 <= 30) {
                    young9++;
                } else if (age20 >= 30 && age20 <= 45) {
                    puber9++;
                } else if (age20 >= 45 && age20 <= 60) {
                    wrinkly9++;
                } else if (age20 >= 60 && age20 < +75) {
                    quinquagenarian9++;
                } else if (age20 >= 75 && age20 <= 90) {
                    oldpeople9++;
                }
            }}


        young.add("15岁-30岁");
        young.add(young1);
        young.add(young2);
        young.add(young3);
        young.add(young4);
        young.add(young5);
        young.add(young6);
        young.add(young7);
        young.add(young8);
        young.add(young9);

        puber.add("30岁-45岁");
        puber.add(puber1);
        puber.add(puber2);
        puber.add(puber3);
        puber.add(puber4);
        puber.add(puber5);
        puber.add(puber6);
        puber.add(puber7);
        puber.add(puber8);
        puber.add(puber9);


        wrinkly.add("45岁-60岁");
        wrinkly.add(wrinkly1);
        wrinkly.add(wrinkly2);
        wrinkly.add(wrinkly3);
        wrinkly.add(wrinkly4);
        wrinkly.add(wrinkly5);
        wrinkly.add(wrinkly6);
        wrinkly.add(wrinkly7);
        wrinkly.add(wrinkly8);
        wrinkly.add(wrinkly9);

        quinquagenarian.add("65岁-80岁");
        quinquagenarian.add(quinquagenarian1);
        quinquagenarian.add(quinquagenarian2);
        quinquagenarian.add(quinquagenarian3);
        quinquagenarian.add(quinquagenarian4);
        quinquagenarian.add(quinquagenarian5);
        quinquagenarian.add(quinquagenarian6);
        quinquagenarian.add(quinquagenarian7);
        quinquagenarian.add(quinquagenarian8);
        quinquagenarian.add(quinquagenarian9);

        oldpeople.add("80岁-95岁");
        oldpeople.add(oldpeople1);
        oldpeople.add(oldpeople2);
        oldpeople.add(oldpeople3);
        oldpeople.add(oldpeople4);
        oldpeople.add(oldpeople5);
        oldpeople.add(oldpeople6);
        oldpeople.add(oldpeople7);
        oldpeople.add(oldpeople8);
        oldpeople.add(oldpeople9);


        List<Object> source = new ArrayList<>();
        source.add(yearList);
        source.add(young);
        source.add(puber);
        source.add(wrinkly);
        source.add(quinquagenarian);
        source.add(oldpeople);

        Map<String, Object> dataset = new HashMap<>();
        dataset.put("source",source);
        return dataset;


    }

    @Override
    public void edit01(Member member) {
        memberDao.edit01(member);
    }


    @Override
    public List<Map<String, Object>> findmemberInfo(int id) {
        List<Map<String, Object>> maps = memberDao.findmemberInfo(id);
        for (Map<String, Object> map : maps) {
            //获取套餐名称
            String setmeal = (String) map.get("setmeal");
            //根据套餐名称查找所有的检查组
            List<Map<String, Object>> checkgroups = memberDao.findAllCheckGroup(setmeal);

            List<String> checkGroups = new ArrayList<>();
            List<String> checkItems = new ArrayList<>();
            for (Map<String, Object> checkgroup : checkgroups) {

                System.out.println("每个检查组集合是:" + checkgroup);

                String checkGroup = (String) checkgroup.get("name");//检查组名称
                checkGroups.add(checkGroup);
                List<Map<String, Object>> allCheckItem = memberDao.findAllCheckItem(checkGroup);//获取所有检查项集合
                for (Map<String, Object> checkItemm : allCheckItem) {

                    System.out.println("每个检查项集合是：" + checkItemm);

                    String checkItem = (String) checkItemm.get("name");
                    checkItems.add(checkItem);
                }

            }

            map.put("checkGroup", checkGroups);
            map.put("checkItem", checkItems);
        }
        return maps;
    }

    //根据手机号验证码修改密码
    @Override
    public void forgetPassword(String id, String password) {
        memberDao.forgetPassword(id,password);
    }




}
